package com.xiaoma.tpo.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.domain.ClsSentence;
import com.xiaoma.tpo.domain.ClsWord;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.tpo.utils.PagingOrder;

@Repository
public class ClsWordDao extends BaseDaoImpl<ClsWord>{

	public List<ClsWord> findAllWithCondition(Integer page, Integer rows,
			Integer clsGateId, String code, String word, String wordCn) {
		// TODO Auto-generated method stub
		return null;
	}

	public Pager findWithCondition(Integer page, Integer rows,
			Integer clsGateId, String code, String word, String wordCn) {
		Pager pager = new Pager();
		Integer startNum = 0;
		if(page != null && page >0){
			startNum = (page-1)*rows;
			pager.setCurPage(rows);
		}
		Integer perNum = 50;
		if(rows != null){
			perNum = rows;
			pager.setItemsPerPage(rows);
		}else{
			pager.setItemsPerPage(50);
		}
		String countSql = " select count(*)"
				+ " from cls_word where 1=1";
		String sql = "select * "
				+ " from cls_word where 1=1";
		if(clsGateId != null){
			sql = sql + " and cls_gate_id = "+clsGateId;
			countSql = countSql + " and cls_gate_id = "+clsGateId;
		}
		if(code != null && code.length() >0){
			sql = sql +" and code = '"+code.trim()+"'";
			countSql = countSql +" and code = '"+code.trim()+"'";
		}
		if(word != null && word.length() > 0){
			sql = sql + " and word like '%"+word.trim()+"%'";
			countSql = countSql + " and word like '%"+word.trim()+"%'";
		}
		if(wordCn != null && wordCn.length() >0){
			sql = sql + " and word_cn like '%"+wordCn.trim()+"%'";
			countSql = countSql + " and word_cn like '%"+wordCn.trim()+"%'";
		}
		sql += " order by id  limit "+startNum+","+perNum;
		countSql += " order by id  ";
		Integer totalCount = jdbcTemplate.queryForInt(countSql);
		List<?> lClsWords = jdbcTemplate.query(sql, new DefaultRowMapper(ClsWord.class, getNameHandler()));
		pager.setList(lClsWords);
		pager.setTotalCount(totalCount);
		return pager;
	}

}
